<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id')->unique()->comment('关联用户ID');
            $table->string('employee_no', 50)->unique()->comment('员工工号');
            $table->string('real_name', 100)->comment('真实姓名');
            $table->enum('gender', ['male', 'female', 'other'])->comment('性别');
            $table->date('birth_date')->comment('出生日期');
            $table->string('id_card', 18)->unique()->comment('身份证号');
            $table->string('phone', 20)->comment('手机号码');
            $table->string('wechat')->nullable()->comment('微信号');
            $table->string('dingtalk')->nullable()->comment('钉钉号');
            $table->string('work_email')->nullable()->comment('工作邮箱');
            $table->text('address')->nullable()->comment('居住地址');
            $table->string('emergency_contact', 100)->nullable()->comment('紧急联系人');
            $table->string('emergency_phone', 20)->nullable()->comment('紧急联系人电话');
            $table->enum('marital_status', ['single', 'married', 'divorced', 'widowed'])->nullable()->comment('婚姻状况');
            $table->json('children_info')->nullable()->comment('子女信息');
            $table->json('health_info')->nullable()->comment('健康信息');
            $table->boolean('is_disabled')->default(false)->comment('是否残疾人');
            $table->string('disability_level')->nullable()->comment('伤残等级');
            $table->string('disability_part')->nullable()->comment('伤残部位');
            $table->boolean('is_work_related_injury')->default(false)->comment('是否工伤相关');
            $table->text('personal_intro')->nullable()->comment('个人简介');
            $table->enum('status', ['probation', 'regular', 'probation_leader', 'resigned', 'retired', 'death', 'suspended', 'long_leave'])->default('probation')->comment('员工状态');
            $table->date('hire_date')->comment('入职日期');
            $table->date('probation_end_date')->nullable()->comment('试用期结束日期');
            $table->date('regular_date')->nullable()->comment('转正日期');
            $table->date('resignation_date')->nullable()->comment('离职日期');
            $table->text('resignation_reason')->nullable()->comment('离职原因');
            $table->timestamps();
            
            // 外键约束
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            
            // 索引
            $table->index(['employee_no', 'status']);
            $table->index(['real_name', 'status']);
            $table->index(['id_card', 'status']);
            $table->index(['phone', 'status']);
            $table->index(['wechat', 'status']);
            $table->index(['dingtalk', 'status']);
            $table->index('hire_date');
            $table->index('status');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('employees');
    }
};
